#include <stdio.h>
#include "cJSON.h"

int main()
{
    char str[512] = "{\"results\":[{\"location\":{\"id\":\"WT3Q0FW9ZJ3Q\",\"name\":\"武汉\",\"country\":\"CN\",\"path\":\"武汉,武汉,湖北,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"now\":{\"text\":\"阴\",\"code\":\"9\",\"temperature\":\"26\"},\"last_update\":\"2025-06-27T20:57:23+08:00\"}]}";

    // 解析json字符串
    cJSON * ret_json = cJSON_Parse(str);
    if ( NULL == ret_json )
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        goto end;
    }
    // 打印json数据
    char * string = cJSON_Print(ret_json);
    printf("%s\n",string);

    const cJSON *results = NULL;
    const cJSON *result = NULL;
    const cJSON *location = NULL;
    const cJSON *now = NULL;
    const cJSON *last_update = NULL;
    results = cJSON_GetObjectItemCaseSensitive(ret_json, "results");
    cJSON_ArrayForEach(result, results)
    {
        location = cJSON_GetObjectItemCaseSensitive(result,"location");
        now = cJSON_GetObjectItemCaseSensitive(result,"now");
        last_update = cJSON_GetObjectItemCaseSensitive(result,"last_update");
    }
    const cJSON *address = cJSON_GetObjectItemCaseSensitive(location,"path");
    const cJSON *weather = cJSON_GetObjectItemCaseSensitive(now,"text");
    const cJSON *temperature = cJSON_GetObjectItemCaseSensitive(now,"temperature");

    printf("地址：%s\n天气：%s\n温度：%s\n最后更新时间：%s\n", address->valuestring, weather->valuestring, temperature->valuestring, last_update->valuestring);

end:
    cJSON_Delete(ret_json);
    return 0;
}